<?php
class Lib_Mediafire extends Zend_Controller_Plugin_Abstract {
	private static $_version = 1;
	private static $_format = 'json';
	public static function get_url($urlRequest = '',$arrParam=array()){
		$url = $urlRequest.'?';
		$i=0;
		foreach ($arrParam as $key=>$param){
			if($i++==0){
				$url.=$key.'='.$param;
			}else{
				$url.='&'.$key.'='.$param;
			}
		}
		if(count($arrParam)>0){
			$url .= '&response_format='.self::$_format.'&version='.self::$_version;
		}else{
			$url .= 'response_format='.self::$_format.'&version='.self::$_version;
		}
	      return $url;
	}
	public static function getInfoByJson($urlRequest = '',$arrParam=array()){
		
		$Url = self::get_url($urlRequest,$arrParam);
		$fp = fopen ( $Url, 'r' );
		$json = stream_get_contents ( $fp );
		$obj = json_decode ( $json,true );
		fclose ( $fp );
		header ( 'Content-type: application/json' );
		return $obj;
	}
	public static function getInfoByXml($urlRequest = '',$arrParam=array()){
		self::$_format='xml';
		$Url = $urlRequest.'?';
		$i=0;
		foreach ($arrParam as $key=>$param){
			if($i++==0){
				$Url.=$key.'='.$param;
			}else{
				$Url.='&'.$key.'='.$param;
			}
		}
		$Url .= '&response_format='.self::$_format.'&version='.self::$_version;
		//$fp = fopen ( $Url, 'r' );
		//print_r($Url);die();
		$json = simplexml_load_file ( $Url );
		//fclose ( $fp );
		return $json;
	}
	//===============================User======================================================================//
	//Get session token
	public static function getSessionToken($arrParam=array()){
		$urlRequest='https://www.mediafire.com/api/user/get_session_token.php';
		$Url=self::get_url($urlRequest,$arrParam);
		$json = self::getInfoByJson($Url);
		return $json['response']['session_token'];
	}
	//Get renew session token
	public static function getRenewSessionToken($arrParam=array()){
		$urlRequest='http://www.mediafire.com/api/user/renew_session_token.php';
		$Url=self::get_url($urlRequest,$arrParam);
		$json = self::getInfoByJson($Url);
		return $json;
	}
	//Get information my file
	public static function getMyFile($arrParam = array()){
		$urlRequest ='http://www.mediafire.com/api/user/myfiles_revision.php';
		$Url=self::get_url($urlRequest,$arrParam);
		$json = self::getInfoByJson($Url);
		return $json;
	}
	//Get login token
	public static function getLoginToken($arrParam = array()){
		$urlRequest = 'https://www.mediafire.com/api/user/get_login_token.php';
		$Url=self::get_url($urlRequest,$arrParam);
		$json = self::getInfoByJson($Url);
		return $json;
	}
}